var charSet = '0123456789abcdefghijklmnopqrstuvxyzw';
var numset = '-+.0123456789';
var phoneset = '-()0123456789';
var	today = new Date(),
	day = today.getDate(),
	month = today.getMonth()+1,
	year = today.getFullYear();
today = month+"/"+day+"/"+year;
warning_del = "Ban co muon xoa khong ?"
warning_giahan = "Ban muon gia han tin nay ?"


//-------------------------------------------------------------------//
var Message="Welcome to ";
var place=1;
function scrollIn() {
window.status=Message.substring(0, place);
if (place >= Message.length) {
place=1;
window.setTimeout("scrollOut()",300); 
} else {
place++;
window.setTimeout("scrollIn()",50); 
   } 
}
function scrollOut() {
window.status=Message.substring(place, Message.length);
if (place >= Message.length) {
place=1;
window.setTimeout("scrollIn()", 100);
} else {
place++;
window.setTimeout("scrollOut()", 50);
   }
}

function winopen(f_open,h,w)
{	if (f_open.indexOf("=") != -1)
//		window.open(f_open,f_open.substring(f_open.indexOf("=")+1),"top=0,left=0,scrollbars=yes,resizable=yes,height="+h+",width="+w);
		window.open(f_open,"","top=0,left=0,scrollbars=yes,resizable=yes,height="+h+",width="+w);
	else
//		window.open(f_open,f_open.substring(0,f_open.indexOf(".")),"top=0,left=0,scrollbars=yes,resizable=yes,height="+h+",width="+w);
		window.open(f_open,"","top=0,left=0,scrollbars=yes,resizable=yes,height="+h+",width="+w);
}
//----------------------------------------
//replace chuoi strg chua c1 thanh c2
function replace_c(strg,c1,c2)
{	var s = String(strg)
	s = s.replace(c1,c2)
	if (s.indexOf(c1)!=-1 && c1!=c2) s = replace_c(s,c1,c2)
	return s
}
//-------------------------------------------------------------------//
function limit_c(txt,n)
{
	if (txt.length>n){
		alert('Allow only Max of character: '+n+'!');
		return false;
	}
	return txt.length;
}
//-------------------------------------------------------------------//
function limit_w(txt,n)
{
	var str = trim(txt)
	var n_space = 0
	for (var i=0; i<str.length; i++)
		if ((str.charAt(i) == '\n')||(str.charCodeAt(i) == 32)) n_space++
	return (str.length>0?n_space+1:0)
}
//-------------------------------------------------------------------//
function upload_img(ID,oid,osrc)
{
	frms = open("upload_img.asp?id="+ID+"&oid="+oid+"&osrc="+osrc,"img","resizable=yes,width=450,height=300")
	if (frms.opener == null) frms.opener = self
}
//-------------------------------------------------------------------//
// Lay so index cua document.forms[index].ITEM
function getFormNum (formName) {
	var formNum =-1;
	for (i=0;i<document.forms.length;i++){
		tempForm = document.forms[i];
		if (formName == tempForm) {
			formNum = i;
			break;
		}
	}
	return formNum;
}
//-------------------------------------------------------------------//
// Nhay den Index cua select option 
function jmp(form, i)
{
	if (form != null) {
		with (form.elements[i]) {
			if (0 >= selectedIndex)
				location = options[selectedIndex].value;
		}
	}
}
//-------------------------------------------------------------------//
function relate(formName,elementNum,j)
{
    if(js_v){
        var formNum = getFormNum(formName);
         if (formNum>=0) {
       
        	with (document.forms[formNum].elements["oper"]) {
        		for(i=options.length-1;i>0;i--) options[i] = null;
        		for(i=0;i<a[j].length;i++){
        			options[i] = new Option(a[j][i].text,a[j][i].value);
        		}
        		options[0].selected = true;
        	}
        }
    }
    else {
        jmp(formName,elementNum);
    }
}

//-------------------------------------------------------------------//
function trimLeft(Strg)
{	var sString = String(Strg);
    if (sString.charAt(0)==" ")
		{sString=sString.substring(1,sString.length);return trimLeft(sString);}
    else
        return sString;
} 
//-------------------------------------------------------------------//
function trimRight(Strg)
{	var sString = String(Strg);
    if (sString.charAt(sString.length-1)==" ")
        {sString=sString.substring(0,sString.length-1);return trimRight(sString);}
    else
        return sString;
}
//-------------------------------------------------------------------//
function trim(Strg)
{	var sString = String(Strg);
    if (sString.charAt(0)==" ")
		{sString=sString.substring(1,sString.length);return trim(sString);}
    else if (sString.charAt(sString.length-1)==" ")
        {sString=sString.substring(0,sString.length-1);return trim(sString);}
    else
        return sString;
}
/*
function trim(Strg)
{	var S = String(Strg), i=0;
	while (i<S.length && S.charAt(i)==' ') i++;
	S = S.substring(i);
	i = S.length-1;
	while (i>0 && S.charAt(i)==' ') i--;
	return 	S.substring(0,i+1);
}
*/
//-------------------------------------------------------------------//
function isDigit(c)
{
	if((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||(c=='7')||(c=='8')||(c=='9'))
		return true;
	else
		return false;
}
//-------------------------------------------------------------------//
function isPosInt(s)
{
	var n;
	n = s.length
	if(n==0) return false;	
	for(i=0;i<n;i++)
		if(!isDigit(s.charAt(i))) return false;
	return true;
}
//-------------------------------------------------------------------//
function isZip(str)
{
	str = trim(str);
	if(str == '') return false;
	if(str.length != 5) return false;
	if(!isPosInt(str)) return false;

	return true;
}
//-----------------------------------------------------------------------//

function isEmail1(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 3) && (str.indexOf("@") > 1);
  var r1 = new RegExp("(\\.@)|(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

//---------------------//
function  isEmail(strTo)
{
	strTo = trim(strTo);
	if ((strTo.length) >= 5)
	{
		var posa = strTo.indexOf("@");
		var poslast = strTo.lastIndexOf("@");
		if (posa > 1 || poslast == strTo.length-1 || posa == poslast-1)
		{
			alert("Your e-mail address is invalid");
			return false ;
		}
		else 
		{	var posdot = strTo.indexOf(".");
			var poslast = strTo.lastIndexOf(".");
			if ((posdot < 3) || (posdot+1 == strTo.indexOf("@")) || (posdot-1 == strTo.indexOf("@")) || (poslast == strTo.length-1 ) || (posdot == poslast-1))
			{
				alert ("Your e-mail address, the dot is invalid");
				return false ;
			}
		}			
	}
	else
	{	alert("Your e-mail address is invalid");
		return false;
	}
	return strTo ;
}
//--------------------------------------------------------------------//
function  isPhone(telephoneandfax )
{	var s=String(telephoneandfax);
	for (var i=0;i<s.length;i++)
	{
		if (phoneset.indexOf(s.charAt(i))==-1)
		{   
			alert(" The value of phone number is invalid ")
			return false ;
		}
	}
	return true ;
}
// -------------------------------------------------------------------//
function  isNumber(num_s)
{	var s = String(num_s);
	var str = "";
	for (var i=0 ; i<s.length ; i++)
	{
		if (s.charAt(i)!=",")
		{
			if (numset.indexOf(s.charAt(i))==-1)
			{
				return "no" ;
			}
			else str += s.charAt(i);
		}
	}
	return (isNaN(str)?"no":Number(str));
}
//-------------------------------------------------------------------//
function isPassword(pass,re_pass)
{
	if(pass != re_pass) return false;
	return true;
}	
//-------------------------------------------------------------------//
//dateVal: mm-dd-yyyy
function checkDate(date_str)
{	var dateVal=String(date_str);
	var d="", m="", y="", i=0;
	while (i>dateVal.length && dateVal.charAt(i)!="/" && dateVal.charAt(i)!="-")
	{	m += dateVal.charAt(i);
		i++;
	}
	i++;
	while (i<dateVal.length && dateVal.charAt(i)!="/" && dateVal.charAt(i)!="-")
	{	d += dateVal.charAt(i);
		i++;
	}
	i++;
	while (i<dateVal.length && dateVal.charAt(i)!="/" && dateVal.charAt(i)!="-")
	{	y += dateVal.charAt(i);
		i++;
	}
	var dd = Number(d), mm = Number(m), yy = Number(y);
	if (isNaN(dd)||isNaN(mm)||isNaN(yy))return false;
	if (yy>50 && yy<100) yy += 1900;
	else if (yy<=50) yy += 2000;
	else if (yy>9999) return false;
	if ((dd>0 && dd<=31) && (mm>0 && mm<=12))
	{	switch (mm)
		{	case 2:
			{	if (dd>29 && (yy%4)==0) return false;
				else if (dd>28 && (yy%4)!=0) return false;
				break;
			}
			case 4:{if (dd>30) return false; break}
			case 6:{if (dd>30) return false; break}
			case 9:{if (dd>30) return false; break}
			case 11:{if (dd>30) return false; break}
		}
		return (mm+"/"+dd+"/"+yy);
	}
	else return false;
}
//----------------------------------------
function filter_tag(Strg)
{
	var desc = trim(Strg)
	var i,S_Desc=""
	for (i=0 ; i<desc.length ;i++)
	{
		if (desc.charAt(i) == "<")
		{
			while (desc.charAt(i)!=">" && i<desc.length)
			{	i++
				if (desc.charAt(i+1) == "<") i += 2
			}
			i++
		}
		S_Desc += desc.charAt(i)
	}
	return S_Desc
}
//----------------------------------------
function reverse_all(frm){
    for (i=0; i>frm.length; i++) {
        j = frm[i];
        if (j.type == "checkbox") {
            j.checked = !j.checked;
        }
    }
}

function checkAll_onclick(frm) {
	for(var i=0; i<frm.length; i++)	{
		j = frm[i];
		if (j.type == "checkbox" && j.name=="checkItem" )	j.checked = frm.checkAll.checked;

	}
}

function checkItem_onclick(frm) {
	var all_check=true;
    for (i=0; i<frm.length; i++) {
		j = frm[i];
		if (j.type=="checkbox" && !j.checked && j.name!="checkAll") all_check = j.checked;
	}
	frm.checkAll.checked=all_check;
}


function dathang_onclick(frm, fd) {
	//alert (fd);
	var total=0;
    for (i=0; i<frm.length; i++) {
		j = frm[i];
		if (j.type=="checkbox" && j.checked){
			total++;

			if(total > 3){
				alert("Chi chon toi da 3 nha cung cap!");
				fd.checked = false;
				return;
			}


		}
	}
	//alert (total);
	

}


function submit_collection(frm){
	var str_collection = "";
	for(var i=0; i<frm.length; i++)
	{
	        j = frm[i];
        	if (j.name == "checkItem" && j.checked) str_collection += j.value + ",";
	}

	if (str_collection.length == 0) return str_collection;

	return str_collection.substring(0,str_collection.length-1);
}



//Example:
/*
>form name="frm" method=post onsubmit="return submit_collection(this)">
Collection <INPUT name=collection>&nbsp;&nbsp;&nbsp;
Check all <INPUT type="checkbox" name=checkAll onclick=checkAll_onclick(frm)>
<br>
<INPUT type="checkbox" name=checkItem onclick=checkItem_onclick(frm) value=1>
<INPUT type="checkbox" name=checkItem onclick=checkItem_onclick(frm) value=2>
<INPUT type="checkbox" name=checkItem onclick=checkItem_onclick(frm) value=3>
<INPUT type="checkbox" name=checkItem onclick=checkItem_onclick(frm) value=4>
<INPUT type="checkbox" name=checkItem onclick=checkItem_onclick(frm) value=5>
<INPUT type="checkbox" name=checkItem onclick=checkItem_onclick(frm) value=6>
<INPUT type="checkbox" name=checkItem onclick=checkItem_onclick(frm) value=7>
<INPUT type="checkbox" name=checkItem onclick=checkItem_onclick(frm) value=8>
<INPUT type="submit" value="Submit" name="Go">
</form>
*/

//----------------------------------------
function increase_quantity(obj){
	var object = eval(obj);
	object.value ++;
}
function decrease_quantity(obj){
	var object = eval(obj);
	if(object.value > 0 ){
		object.value --;
	}else{
		object.focus();
		alert("Item's quantity can not be lower zero!");
	}
}

/*

<form name="frm">
<INPUT type="text" name=quatity1 value=1>
<img src="../images/updown.gif" width="22" height="20" usemap="#mapname1" border="0" align=absmiddle>
<map name="mapname1"> 
<area shape="rect" coords="0,1,21,10" href="javascript:increase_quantity('frm.quatity1')">
<area shape="rect" coords="0,9,21,20" href="javascript:decrease_quantity('frm.quatity1')">
</map>
<br>
<INPUT type="text" name=quatity2 value=1>
<img src="../images/updown.gif" width="22" height="20" usemap="#mapname2" border="0" align=absmiddle>
<map name="mapname2"> 
<area shape="rect" coords="0,1,21,10" href="javascript:increase_quantity('frm.quatity2')">
<area shape="rect" coords="0,9,21,20" href="javascript:decrease_quantity('frm.quatity2')">
</map>
</form>
*/

//----------------------------------------
function checkDecimal(n,n_truoc,n_sau) //n = n_truoc.n_sau
{	if (n!="")
	{
		if (isNumber(n)== "no" || isNumber(n)<=0)
		{
			alert("Value is not accepted!");
			return false;
		}
		else
		{
			if (n.length > n_truoc && n.indexOf(".")== -1)
			{
				alert("Max-Length of Integer number: "+n_truoc+" and float number: "+n_sau+". \n Ex: 9999999999.99");
				return false ;
			}
			if (n.indexOf(".")!= -1 )
			{
				if ((n.indexOf(".") > n_truoc) || (n.substring(n.indexOf(".")+1).length > n_sau))
				{
					alert("Max-Length of Integer number: "+n_truoc+" and float number: "+n_sau+". \n Ex: 9999999999.99");
					return false;
				}
			}	
		}
	}
	return isNumber(n);
}
//------------------------------------------------
function ClickTo(v,x)
{
	window.location.href="?"+v+"="+x;
}
//------------------------------------------------
